import { createRouter, createWebHistory } from 'vue-router';
import notify from '@/api/notify';
import { userStore } from '../stores/user';

const routes = [
    {
        path: '/',
        name: '',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../pages/auth/login.vue'),
        meta: {
            title: '登录页面',
            freeAuth: true,
        }
    },
    {
        path: '/user',
        name: 'user',
        component: () => import('../pages/user/main.vue'),
        meta: {
            title: '侧边栏页面',
            freeAuth: false,
        },
        redirect: '/user/home',
        children: [
            {
                path: 'home',
                name: 'home',
                component: () => import('../pages/user/home.vue'),
                meta: {
                    title: '用户主界面',
                    freeAuth: false,
                }
            },
            {
                path: 'myInfo',
                name: 'myInfo',
                component: () => import('../pages/user/myInfo.vue'),
                meta: {
                    title: '个人信息界面',
                    freeAuth: false,
                }
            },
            {
                path: 'myDepartment',
                name: 'myDepartment',
                component: () => import('../pages/user/myDepartment.vue'),
                meta: {
                    title: '我的科室界面',
                    freeAuth: false,
                }
            },
            {
                path: 'myPatient',
                name: 'myPatient',
                component: () => import('../pages/user/myPatient.vue'),
                meta: {
                    title: '我的患者界面',
                    freeAuth: false,
                }
            },
            {
                path:'recordDetails/:recordId',
                name:'recordDetails',
                component: () => import('../pages/user/recordDetails.vue'),
                meta: {
                    title: '患者记录详情界面',
                    freeAuth: false,
                }
            },
        ]
    },
    {
        path: '/manager',
        name: 'manager',
        component: () => import('../pages/manager/main.vue'),
        meta: {
            title: '管理者',
            freeAuth: false,
            requireAuth: true
        },
        redirect: '/manager/panel',
        children: [
            {
                path: 'panel',
                name: 'panel',
                component: () => import('../pages/manager/panel.vue'),
                meta: {
                    title: '管理者主界面',
                    freeAuth: false,
                    requireAuth: true
                }
            },
            {
                path: 'userManagement',
                name: 'userManagement',
                component: () => import('../pages/manager/userManagement.vue'),
                meta: {
                    title: '管理用户界面',
                    freeAuth: false,
                    requireAuth: true
                }
            },
            {
                path: 'appropvals',
                name: 'appropvals',
                component: () => import('../pages/manager/appropvals.vue'),
                meta: {
                    title: '用户身份审核界面',
                    freeAuth: false,
                    requireAuth: true
                }
            },
            {
                path: 'deptManagement',
                name: 'deptManagement',
                component: () => import('../pages/manager/departmentManagement.vue'),
                meta: {
                    title: '科室管理界面',
                    freeAuth: false,
                    requireAuth: true
                }
            },
            {
                path: 'patientManagement',
                name: 'patientManagement',
                component: () => import('../pages/manager/patientManagement.vue'),
                meta: {
                    title: '患者管理界面',
                    freeAuth: false,
                    requireAuth: true
                }
            }
        ]
    }
];
const router = createRouter({
    history: createWebHistory(),
    routes
});
// 导航守卫
router.beforeEach((to, from, next) => {
    // 从 localStorage 中读取 token
    const token = localStorage.getItem('token');
    // 获取当前路由最后一条匹配记录的 meta 信息
    const currentRecord = to.matched[to.matched.length - 1];
    const freeAuthMeta = currentRecord.meta.freeAuth;
    const requireAuthMeta = currentRecord.meta.requireAuth;
    // 通过状态管理获取用户信息
    const user = userStore();
    // 如果不是免认证的页面并且没有 token，则跳转到首页
    if (!freeAuthMeta && !token) {
        notify.error('请先登录！');
        return next({ name: 'login' });
    }
    // 如果当前页面需要管理员权限，并且用户的权限不满足要求，则跳转到首页
    if (requireAuthMeta && user.userRole !== '1') {
        notify.error('你没有权限访问该页面！');
        return next({ name: 'login' });
    }
    // 通过验证，允许访问目标路由
    next();
});
export default router;
